# Powered by Python 2.7

# To cancel the modifications performed by the script
# on the current graph, click on the undo button.

# Some useful keyboards shortcuts : 
#   * Ctrl + D : comment selected lines.
#   * Ctrl + Shift + D  : uncomment selected lines.
#   * Ctrl + Space  : run script.
#   * Ctrl + F  : find selected text.
#   * Ctrl + R  : replace selected text.

from tulip import *

# the updateVisualization(centerViews = True) function can be called
# during script execution to update the opened views

# the pauseScript() function can also be called to pause the script execution.
# To resume the script execution, you will have to click on the "Run script " button.

# the main(graph) function must be defined 
# to run the script on the current graph

def makeTreeA(graph,viewColor, viewLabel):
	
	tree1 = tlp.newSubGraph(graph, "firstTree");
	Nroot = tree1.addNode();
	#viewColor.setNodeValue(Nroot, tlp.Color(0,0,255));
	#viewLabel.setNodeValue(Nroot, "b");
	N1 = tree1.addNode();
	N2 = tree1.addNode();
	N3 = tree1.addNode();
	N4 = tree1.addNode();
	N5 = tree1.addNode();
	N21 = tree1.addNode();
	N31 = tree1.addNode();
	
	tree1.addEdge(Nroot, N1);
	tree1.addEdge(Nroot, N2);
	tree1.addEdge(Nroot, N3);
	tree1.addEdge(Nroot, N4);
	tree1.addEdge(Nroot, N5);
	tree1.addEdge(N2, N21);
	tree1.addEdge(N3, N31);
	
	return tree1;

def makeTreeB(graph, viewColor, viewLabel):
	
	tree2 = tlp.newSubGraph(graph, "secondTree");
	Nroot = tree2.addNode();
	#viewColor.setNodeValue(Nroot, tlp.Color(0,0,255));
	#viewLabel.setNodeValue(Nroot, "b");
	N1 = tree2.addNode();
	N2 = tree2.addNode();
	N3 = tree2.addNode();
	
	N21 = tree2.addNode();
	N31 = tree2.addNode();
	N32 = tree2.addNode();
	N33 = tree2.addNode();
	N321 = tree2.addNode();
		
	tree2.addEdge(Nroot, N1);
	tree2.addEdge(Nroot, N2);
	tree2.addEdge(Nroot, N3);
	tree2.addEdge(N2, N21);
	tree2.addEdge(N3, N31);
	tree2.addEdge(N3, N32);
	tree2.addEdge(N3, N33);
	tree2.addEdge(N33, N321);
	
	return tree2;

def mGMM(T1, T2,graph, viewLabel) :
	tmpGraph = tlp.newSubGraph(graph, "tmpGraph");
	Ns = tmpGraph.addNode();
	viewLabel.setNodeValue(Ns, "s");
	Nt = tmpGraph.addNode();
	viewLabel.setNodeValue(Nt, "t");
	Nei = tmpGraph.addNode();
	viewLabel.setNodeValue(Nei, "ei");
	Nej = tmpGraph.addNode();
	viewLabel.setNodeValue(Nej, "ej");
	
	listNk = [];
	listNl = [];
	
	rootT1 = T1.getNode(0);
	for k in T1.getNodes() :
		print("forT1");
		Nk = tmpGraph.addNode();	
		viewLabel.setNodeValue(Nk, "k" + str(k));
		listNk.append(Nk);
		Esk = tmpGraph.addEdge(Ns, Nk);
#		viewDouble.setEdgeValue(Esk, 0);
#		viewInteger.setEdgeValue(Esk, 1);
		

		Esei = tmpGraph.addEdge(Ns, Nei);
#		viewDouble.setEdgeValue(Esei, 0);
#		viewInteger.setEdgeValue(Esei, l);
		
		Eejt = tmpGraph.addEdge(Nej, Nt);
#		viewDouble.setEdgeValue(Eejt, 0);
#		viewInteger.setEdgeValue(Eejt, k);
		
		Eeiej = tmpGraph.addEdge(Nei, Nej);
#		viewDouble.setEdgeValue(Eeiej, 0);
#		viewInteger.setEdgeValue(Eeiej, max(k, l) - min (k, l));
		
	for l in T2.getNodes() :
		Nl = tmpGraph.addNode();
		viewLabel.setNodeValue(Nl, "l" + str(l));
		listNl.append(Nl);
		
		Eeil = tmpGraph.addEdge(Nei, Nl);
#			viewDouble.setEdgeValue(Eeil, DT(T2.getSubGraph(l)));
#			viewInteger.setEdgeValue(Eeil, 1);
		
		Elt = tmpGraph.addEdge(Nl, Nt);
#			viewDouble.setEdgeValue(Elt, 0);
#			viewInteger.setEdgeValue(Elt, 1);
		for k in range(len(listNk)):
			
			Ekl = tmpGraph.addEdge(listNk[k], Nl);
#			viewDouble.setEdgeValue(Ekl, DTT(T1.getSubGraph(k), T2.getSubGraph(l)));
#			viewInteger.setEdgeValue(Ekl, 1);
		
			
			
			Ekej = tmpGraph.addEdge(listNk[k], Nej);
#			viewDouble.setEdgeValue(Ekej, DT(T1.getSubGraph(k)));
#			viewInteger.setEdgeValue(Ekej, 1);
			
		
		
	return tmpGraph;	
		
def initLayout(viewLayout) :
	dataSet = tlp.getDefaultPluginParameters("Tree Leaf", graph)
	dataSet["3D layout"] = False;
	graph.computeLayoutProperty("Tree Leaf", viewLayout, dataSet);
	viewLayout.scale(tlp.Coord(.5,.5,.5));
	
def main(graph) : 
	graph.clear();
	viewBorderColor =  graph.getColorProperty("viewBorderColor")
	viewBorderWidth =  graph.getDoubleProperty("viewBorderWidth")
	viewColor =  graph.getColorProperty("viewColor")
	viewFont =  graph.getStringProperty("viewFont")
	viewFontSize =  graph.getIntegerProperty("viewFontSize")
	viewLabel =  graph.getStringProperty("viewLabel")
	viewLabelColor =  graph.getColorProperty("viewLabelColor")
	viewLabelPosition =  graph.getIntegerProperty("viewLabelPosition")
	viewLayout =  graph.getLayoutProperty("viewLayout")
	viewMetaGraph =  graph.getGraphProperty("viewMetaGraph")
	viewRotation =  graph.getDoubleProperty("viewRotation")
	viewSelection =  graph.getBooleanProperty("viewSelection")
	viewShape =  graph.getIntegerProperty("viewShape")
	viewSize =  graph.getSizeProperty("viewSize")
	viewSrcAnchorShape =  graph.getIntegerProperty("viewSrcAnchorShape")
	viewSrcAnchorSize =  graph.getSizeProperty("viewSrcAnchorSize")
	viewTexture =  graph.getStringProperty("viewTexture")
	viewTgtAnchorShape =  graph.getIntegerProperty("viewTgtAnchorShape")
	viewTgtAnchorSize =  graph.getSizeProperty("viewTgtAnchorSize")

	tree1 = makeTreeA(graph, viewColor, viewLabel);
	tree2 = makeTreeB(graph, viewColor, viewLabel);
	tree3 = mGMM(tree1, tree2, graph, viewLabel);
	
	initLayout(viewLayout);
	
	
	return True;